#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "msg.h"
#include "handler_serial.h"

extern int dev_uart_fd;
extern int msgid;
struct msg buf_recv;
int fd;
int alarm_temp = 30;
#define BEEP 0X10

/**
 *@breif:下行命令处理线程
 *@desc:从消息队列中读取命令
 *@author：马德顺
 */
void *handler_cmd(void * arg)
{
	msg_init();

	while(1)
	{
		if(msgrcv(msgid,&buf_recv,SIZE,1,0) == -1)
		{
			perror("msgrcv err!");
			return ;
		}

		printf("msg get--->%d\n",(int)buf_recv.text);

		//开led2灯
		if((int)buf_recv.text == 0x61)
		{
			system("echo 1 > /sys/class/leds/led2/brightness");
		}

		//关led2灯
		if((int)buf_recv.text == 0x60)
		{
			system("echo 0 > /sys/class/leds/led2/brightness");
		}

		//开led3灯
		if((int)buf_recv.text == 0x63)
		{
			system("echo 1 > /sys/class/leds/led3/brightness");
		}

		//关led3灯
		if((int)buf_recv.text == 0x62)
		{
			system("echo 0 > /sys/class/leds/led3/brightness");
		}

		//开蜂鸣器
		if((int)buf_recv.text == 0x51)
		{
			if((fd = open("/dev/beep", O_RDWR)) < 0)
			{
				printf("open device beep failed\n");
			}
			ioctl(fd, BEEP, 1);
			close(fd);
		}

		//关蜂鸣器
		if((int)buf_recv.text == 0x50)
		{
			if((fd = open("/dev/beep", O_RDWR)) < 0)
			{
				printf("open device beep failed\n");
			}
			ioctl(fd, BEEP, 0);
			close(fd);
		}

		//开m0led灯
		if((int)buf_recv.text == 0xa1)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xa1;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//关m0led灯
		if((int)buf_recv.text == 0xa0)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xa0;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//开beep
		if((int)buf_recv.text == 0x91)
		{
			open_serial();
			unsigned char dev_uart_mask = 0x91;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//关beep
		if((int)buf_recv.text == 0x90)
		{
			open_serial();
			unsigned char dev_uart_mask = 0x90;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>0
		if((int)buf_recv.text == 0xb0)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb0;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>1
		if((int)buf_recv.text == 0xb1)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb1;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>2
		if((int)buf_recv.text == 0xb2)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb2;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>3
		if((int)buf_recv.text == 0xb3)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb3;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>4
		if((int)buf_recv.text == 0xb4)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb4;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>5
		if((int)buf_recv.text == 0xb5)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb5;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>6
		if((int)buf_recv.text == 0xb6)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb6;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>7
		if((int)buf_recv.text == 0xb7)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb7;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>8
		if((int)buf_recv.text == 0xb8)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb8;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管点亮>9
		if((int)buf_recv.text == 0xb9)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xb9;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//数码管关闭
		if((int)buf_recv.text == 0xbf)
		{
			open_serial();
			unsigned char dev_uart_mask = 0xbf;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//风扇1档
		if((int)buf_recv.text == 0x81)
		{
			open_serial();
			unsigned char dev_uart_mask = 0x81;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//风扇2档
		if((int)buf_recv.text == 0x82)
		{
			open_serial();
			unsigned char dev_uart_mask = 0x82;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//风扇3档
		if((int)buf_recv.text == 0x83)
		{
			open_serial();
			unsigned char dev_uart_mask = 0x83;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		//风扇关闭
		if((int)buf_recv.text == 0x80)
		{
			open_serial();
			unsigned char dev_uart_mask = 0x80;
			printf("send to m0--->%x\n",(int)dev_uart_mask);
			write(dev_uart_fd, &dev_uart_mask, 1);
			close(dev_uart_fd);
		}

		

	}

	
	
	pthread_exit("tid exit");
	
}
